#include "comm.hpp"
#include <unistd.h>

int main()
{
    key_t k = getKey();
    printf("key: 0x%x\n", k);
    int shmid = creatShm(k);
    printf("shmid: %d\n", shmid);

    // sleep(5);

    // 关联
    char *start = (char*)attachShm(shmid);
    printf("attach success, address start: %p\n", start);

    //关联后可以使用
    while(true)
    {
        printf("client say: %s\n", start);
        struct shmid_ds ds;
        shmctl(shmid, IPC_STAT, &ds);
        printf("获取属性: size: %d, pid: %d, myself: %d, key: 0x%x", ds.shm_segsz, ds.shm_cpid, getpid(), ds.shm_perm.__key);
        sleep(1);
    }

    // 去关联 -> 并不是删除,是将共享内存与进程的联系断开 -> 修改页表,回收内存空间
    detachShm(start);
    sleep(10);

    // 删除
    delShm(shmid);
    return 0;
}